/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.MenuImg;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Comment;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.ImageTag;
import com.simon.mengkou.data.bean.base.Tag;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.bean.req.ShareOrderLikeReq;
import com.simon.mengkou.data.enums.EDirection;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.adapter.CommentAdapter;
import com.simon.mengkou.ui.adapter.ProductImageAdapter;
import com.simon.mengkou.ui.adapter.ShareOrderImageAdapter;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.ui.view.BaseViewPager;
import com.simon.mengkou.ui.view.CollectionButton;
import com.simon.mengkou.ui.view.FollowButton;
import com.simon.mengkou.ui.view.LoopPageIndicator;
import com.simon.mengkou.ui.view.LoopViewPager;
import com.simon.mengkou.ui.view.LoopViewPagerAdapter;
import com.simon.mengkou.ui.view.flowlayout.FlowLayout;
import com.simon.mengkou.ui.view.label.AnimateLabelView;
import com.simon.mengkou.ui.view.label.LabelInfo;
import com.simon.mengkou.utils.UtilOuer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController2;
import io.vov.vitamio.widget.VideoView2;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/27.
 * desc    : 晒单详情界面
 */
public class ShareOrderDetailActivity extends BaseTopActivity
        implements PullToRefreshBase.OnRefreshListener2<ListView>{

    private RelativeLayout mRlBannerRoot;
    //单张商品图片
    private SimpleDraweeView mSdvImage;
    //多张商品图片
    private LoopViewPager mLvpPager;
    private LoopViewPagerAdapter mPagerAdapter;
    private LoopPageIndicator mLvpIndicator;
    private ShareOrderImageAdapter mImageAdapter;

    //头像
    private AvatarSimpleDraweeView mSdvAvatar;
    //昵称
    private TextView mTvName;
    //时间
    private TextView mTvTime;
    //关注
    private FollowButton mFbFollow;
    //内容
    private TextView mTvContent;
    //标签
    private FlowLayout mFlTags;
    //喜欢
    private TextView mIvComment;
    //喜欢
    private TextView mIvLike;

    //喜欢
    private TextView mIvRead;
    //
    private ImageView mIvVideo;
    private VideoView2 mVvVideo;

    //评论
    @Bind(R.id.comment_id_content)
    EditText mEtContent;
    //评论列表
    @Bind(R.id.common_pulltorefresh_id_list)
    PullToRefreshListView mPtrView;


    private CommentAdapter mAdapter;
    private String mMaxId = CstOuer.PAGE.DEFAULT_MAXID;
    private Follow mFollow;
    private String mId;

    private Comment mReplyComment;

    private MenuImg mCollectedMenu;
    private MenuImg mUncollectedMenu;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        mFollow = (Follow) intent.getSerializableExtra(CstOuer.KEY.FOLLOW);
        mId = intent.getStringExtra(CstOuer.KEY.ID);
        if(mFollow != null) {
            mId = mFollow.getId();
        }
    }

    @Override
    protected void initTop() {
        setNavigation(R.drawable.common_ic_left_nav);


        mCollectedMenu = new MenuImg.MenuImgBuilder(this)
                .setIcon(R.drawable.common_ic_collected)
                .setListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        addMenu(mUncollectedMenu);
                        hideMenu(mCollectedMenu);
                        UtilOuer.toast(ShareOrderDetailActivity.this, R.string.share_video_collection_failure_tips);
                        mFollow.setCollected(true);
                        OuerApplication.mUser.setTopicNum(OuerApplication.mUser.getTopicNum() - 1);

                        AgnettyFuture future = OuerApplication.mOuerFuture.videoFollow(mFollow.getId(), false, null);
                        attachDestroyFutures(future);

                        //发送视频收藏广播
                        OuerDispatcher.sendVideoFollowBroadcast(ShareOrderDetailActivity.this, mFollow.getId());
                        return false;
                    }
                }).build();

        mUncollectedMenu = new MenuImg.MenuImgBuilder(this)
                .setIcon(R.drawable.common_ic_uncollected)
                .setListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        addMenu(mCollectedMenu);
                        hideMenu(mUncollectedMenu);
                        UtilOuer.toast(ShareOrderDetailActivity.this, R.string.share_video_collection_tips);
                        mFollow.setCollected(true);
                        OuerApplication.mUser.setTopicNum(OuerApplication.mUser.getTopicNum() + 1);

                        AgnettyFuture future = OuerApplication.mOuerFuture.videoFollow(mFollow.getId(), true, null);
                        attachDestroyFutures(future);

                        //发送视频收藏广播
                        OuerDispatcher.sendVideoFollowBroadcast(ShareOrderDetailActivity.this, mFollow.getId());
                        return false;
                    }
                }).build();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_share_order_detail);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        mPtrView.setOnRefreshListener(this);
        mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
        mPtrView.setTag(false);

        //头部
        View header = LayoutInflater.from(this).inflate(R.layout.layout_share_order_header, null);
        mRlBannerRoot = (RelativeLayout)header.findViewById(R.id.product_id_banner_root);
        mSdvImage = (SimpleDraweeView)header.findViewById(R.id.product_id_image);
        mIvVideo = (ImageView)header.findViewById(R.id.product_id_video);
        mVvVideo = (VideoView2)header.findViewById(R.id.surface_view);
        mLvpIndicator = (LoopPageIndicator)header.findViewById(R.id.product_id_indicator);
        mLvpPager = (LoopViewPager) header.findViewById(R.id.product_id_banner);
        mLvpPager.setAutoScroll(false);


        //头像
        mSdvAvatar = (AvatarSimpleDraweeView) header.findViewById(R.id.follow_id_avatar);
        //昵称
        mTvName = (TextView) header.findViewById(R.id.follow_id_name);
        //时间
        mTvTime = (TextView) header.findViewById(R.id.follow_id_time);
        //关注
        mFbFollow = (FollowButton) header.findViewById(R.id.follow_id_btn);
        //内容
        mTvContent = (TextView) header.findViewById(R.id.follow_id_content);
        //标签
        mFlTags = (FlowLayout) header.findViewById(R.id.follow_id_tags);
        //评论
        mIvComment = (TextView) header.findViewById(R.id.follow_id_comment);
        //喜欢
        mIvLike = (TextView) header.findViewById(R.id.follow_id_like);
        //阅读
        mIvRead = (TextView) header.findViewById(R.id.follow_id_read);


        mPtrView.getRefreshableView().addHeaderView(header);

        mAdapter = new CommentAdapter(this, this);
        mPtrView.setAdapter(mAdapter);


        //获取晒单详情
        if(mFollow != null) {
            setShareOrderDetail(mFollow);
            //获取评论数据
            getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);
        } else {
            setOnRetryListener(new OnRetryListener() {
                @Override
                public void onRetry() {
                    getShareOrderDetail();
                }
            });

            getShareOrderDetail();
        }

        registerAction(CstOuer.BROADCAST_ACTION.SHARE_ORDER_COMMENT_ACTION);
        registerAction(CstOuer.BROADCAST_ACTION.SHARE_ORDER_LIKE_ACTION);
    }


    @Override
    protected void onPause() {
        super.onPause();
        if(mVvVideo.getVisibility() == View.VISIBLE ) {
            mVvVideo.stopPlayback();
            mVvVideo.setVisibility(View.GONE);
            mIvVideo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        if(CstOuer.BROADCAST_ACTION.SHARE_ORDER_LIKE_ACTION.equals(intent.getAction())) {
            ShareOrderLikeReq req = (ShareOrderLikeReq) intent.getSerializableExtra(CstOuer.KEY.LIKE);
            if(mFollow != null && mFollow.getId().equals(req.getId())) {
                mFollow.setLiked(req.isCheck());
                if(mFollow.isLiked()) {//喜欢
                    mFollow.setFaverNum(mFollow.getFaverNum() + 1);
                    mIvLike.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_like, 0, 0, 0);
                } else {//不喜欢
                    mFollow.setFaverNum(mFollow.getFaverNum() - 1);
                    mIvLike.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_unlike, 0, 0, 0);
                }

                mIvLike.setText(UtilOuer.formatNum(this, mFollow.getFaverNum()));
            }
        } else if(CstOuer.BROADCAST_ACTION.SHARE_ORDER_COMMENT_ACTION.equals(intent.getAction())) {
            String id =  intent.getStringExtra(CstOuer.KEY.ID);
            if(mFollow.getId().equals(id)) {
                mFollow.setCommentNum(mFollow.getCommentNum() + 1);
                mIvComment.setText(UtilOuer.formatNum(this, mFollow.getCommentNum()));
            }
        }
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        //下拉刷新
        getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        //上拉加载更多
        getData(mMaxId, EDirection.LOADMORE);
    }

    /**
     * 获取晒单详情
     */
    private void getShareOrderDetail() {
        AgnettyFuture future = OuerApplication.mOuerFuture.shareOrderDetail(mId,
                new OuerFutureListener(this) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setLoading(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setLoading(false);
                        mFollow = (Follow) result.getAttach();
                        setShareOrderDetail(mFollow);
                        //获取评论数据
                        getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setRetry(true);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setRetry(true);
                    }
                }
            );
        attachDestroyFutures(future);
    }


    /**
     * 设置晒单详情
     */
    private void setShareOrderDetail(final Follow data) {
        final User user = data.getUser();

        if(UtilString.isNotBlank(data.getVideoUrl())) {
            mIvVideo.setVisibility(View.VISIBLE);
            setTitle(R.string.share_video_title);

            //收藏
            if (!data.isCollected()) {
                addMenu(mUncollectedMenu);
                hideMenu(mCollectedMenu);
            } else {
                addMenu(mCollectedMenu);
                hideMenu(mUncollectedMenu);
            }


            int width = UtilDevice.getDevice(this).getWidth();
            mVvVideo.getLayoutParams().height = (int) (width/2f);
            mVvVideo.setVisibility(View.GONE);
            mRlBannerRoot.getLayoutParams().height = (int) (width/2f);

            mSdvImage.setAspectRatio(2f);
            OuerApplication.mImageLoader.loadImage(mSdvImage, data.getVideoCover());
            mIvVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playVideo(mIvVideo, data);
                }
            });
        } else {
            mIvVideo.setVisibility(View.GONE);
            setTitle(R.string.share_title);


            //图片
            updateBanner(data.getImageArray());
        }

        //关注
        if(user.getUserId().equals(OuerApplication.mUser.getUserId())) {
            mFbFollow.setVisibility(View.GONE);
        } else {
            mFbFollow.setVisibility(View.VISIBLE);
            mFbFollow.follow(user.getInterested());
            mFbFollow.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    user.setInterested(!user.getInterested());
                    mFbFollow.follow(user.getInterested());

                    if (user.getInterested()) {
                        OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() + 1);
                    } else {
                        OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() - 1);
                    }

                    AgnettyFuture future = OuerApplication.mOuerFuture.follow(
                            OuerApplication.mUser.getUserId(), user.getUserId(), user.getInterested(), null);
                    attachDestroyFutures(future);

                    //发送关注or取消关注广播
                    OuerDispatcher.sendFollowBroadcast(ShareOrderDetailActivity.this, user.getUserId(),
                            user.getInterested());
                }
            });
        }


        //头像
        mSdvAvatar.loadAvatar(user.getAvatarUrl(), user.getLevel());
        mSdvAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开用户详情
                OuerDispatcher.presentUserActivity(ShareOrderDetailActivity.this,
                        user.getUserId(), user.getNick());
            }
        });
        //昵称
        mTvName.setText(user.getNick());
        //时间
        mTvTime.setText(UtilOuer.getFormatDate(this, data.getCreateAt(), "yy-M-d"));



        //内容
        mTvContent.setText(data.getContent());

        //标签
        mFlTags.removeAllViews();
        List<Tag> tags = data.getTags();
        if(UtilList.isNotEmpty(tags)) {
            mFlTags.setVisibility(View.VISIBLE);
            for(final Tag tag : tags) {
                TextView tvTag = new TextView(this);
                tvTag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                tvTag.setBackgroundResource(R.drawable.share_order_ic_tag);
                tvTag.setSingleLine(true);
                tvTag.setEllipsize(TextUtils.TruncateAt.END);
                tvTag.setText(tag.getName());
                tvTag.setTextColor(ContextCompat.getColor(this, R.color.res_color_black_secondary_text));
                mFlTags.addView(tvTag);

                tvTag.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        //打开标签聚合界面
                        OuerDispatcher.presentTagPostsActivity(ShareOrderDetailActivity.this, tag.getId(), tag.getName());
                    }
                });
            }
        } else {
            mFlTags.setVisibility(View.GONE);
        }


        mIvComment.setText(UtilOuer.formatNum(this, data.getCommentNum()));
        mIvRead.setText(UtilOuer.formatNum(this, data.getReadNum() + 1));


        if(data.isLiked()) {//喜欢
            mIvLike.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_like, 0, 0, 0);
        } else {//不喜欢
            mIvLike.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_unlike, 0, 0, 0);
        }

        mIvLike.setText(UtilOuer.formatNum(this, data.getFaverNum()));


        mIvLike.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                data.setLiked(!data.isLiked());

                AgnettyFuture future = OuerApplication.mOuerFuture.shareOrderLike(data.getId(),
                        data.isLiked(), null);
                attachDestroyFutures(future);

                ShareOrderLikeReq req = new ShareOrderLikeReq();
                req.setId(data.getId());
                req.setCheck(data.isLiked());
                OuerDispatcher.sendShareOrderLikeBroadcast(ShareOrderDetailActivity.this, req);
            }
        });


        postRead(data.getId(), 1);

    }


    public void playVideo(final View ivVideo, final Follow follow) {
        String url = follow.getVideoUrl();
        if(UtilString.isBlank(url)) {
            UtilOuer.toast(this, R.string.video_player_error);
            return;
        }

        ivVideo.setVisibility(View.GONE);
        mVvVideo.setVisibility(View.VISIBLE);
        mVvVideo.setVideoPath(url);
        MediaController2 mediaController = new MediaController2(this);

        mediaController.setOnNavListener(new MediaController2.OnNavListener() {
            @Override
            public void onNav() {
                mVvVideo.stopPlayback();
                mVvVideo.setVisibility(View.GONE);
                ivVideo.setVisibility(View.VISIBLE);
            }
        });

        mediaController.setOnFullScreenListener(new MediaController2.OnFullScreenListener() {
            @Override
            public void onFullScreen() {
                OuerDispatcher.presentVideoPlayerActivity(ShareOrderDetailActivity.this,
                        follow.getId(), follow.getVideoUrl());
                mVvVideo.setVisibility(View.GONE);
                ivVideo.setVisibility(View.VISIBLE);
                mVvVideo.stopPlayback();
            }
        });

        mVvVideo.setMediaController(mediaController);
        mVvVideo.requestFocus();

        mVvVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });

        mVvVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mVvVideo.setVisibility(View.GONE);
                ivVideo.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 更新商品图片
     * @param datas
     */
    private void updateBanner(List<Image> datas) {
        if (UtilList.isNotEmpty(datas)) {

            if(UtilList.getCount(datas) == 1) {
                mSdvImage.setAspectRatio(1f);
                OuerApplication.mImageLoader.loadImage(mSdvImage, datas.get(0).getUrl());

                final RelativeLayout rlTagRoot = (RelativeLayout)mRlBannerRoot.findViewById(R.id.product_id_img_tag_root);
                rlTagRoot.getLayoutParams().height = UtilDevice.getDevice(this).getWidth();
                rlTagRoot.removeAllViews();

                rlTagRoot.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for(int i=0; i<rlTagRoot.getChildCount(); i++) {
                            AnimateLabelView label = (AnimateLabelView) rlTagRoot.getChildAt(i);
                            if(label.getVisibility() == View.VISIBLE) {
                                label.setVisibility(View.GONE);
                            } else {
                                label.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                });

                List<ImageTag> tags = datas.get(0).getTags();
                if(UtilList.isNotEmpty(tags)) {
                    for (ImageTag tag : tags) {
                        LabelInfo info = new LabelInfo();
                        info.title1Text = tag.getBrand();
                        info.title2Text = tag.getPrice();
                        info.title3Text = tag.getLocation();
                        info.pcX = tag.getXratio();
                        info.pcY = tag.getYratio();
                        AnimateLabelView label = new AnimateLabelView(this);
                        label.setLabelInfo(info);
                        label.alwaysWave();

                        rlTagRoot.addView(label);
                    }
                }
            } else {
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mRlBannerRoot.getLayoutParams();
                params.width = UtilDevice.getDevice(this).getWidth();
                params.height = params.width;

                mLvpIndicator.initIndicator(0,
                        datas.size(),
                        R.drawable.common_ic_indicator_dot_normal,
                        R.drawable.common_ic_indicator_dot_focus);

                mImageAdapter = new ShareOrderImageAdapter(this, datas);
                mPagerAdapter = new LoopViewPagerAdapter(mImageAdapter);
                mLvpPager.setInfinateAdapter(mPagerAdapter);

                mLvpPager.setOnPageChangeListener(new BannerPageChangeListener(datas.size()));
            }
        }
    }

    /**
     * 获取评论数据
     * @param maxId
     * @param direction
     */
    private void getData(final String maxId, final int direction) {
        AgnettyFuture future = OuerApplication.mOuerFuture.shareOrderComments(
                maxId, CstOuer.PAGE.DEFAULT_MINID, CstOuer.PAGE.DEFAULT_SIZE, mId,
                new OuerFutureListener(this){

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        mPtrView.onRefreshComplete();

                        List<Comment> datas = (List<Comment>) result.getAttach();
                        int count = UtilList.getCount(datas);
                        //设置最新的max id
                        if(count != 0) {
                            mMaxId = datas.get(count - 1).getId();
                        }

                        //数据已读取完
                        if(count < CstOuer.PAGE.DEFAULT_SIZE) {
                            mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
                        } else {
                            mPtrView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
                        }

                        if(direction == EDirection.REFRESH) {
                            mAdapter.setList(datas);
                        } else {
                            mAdapter.addList(datas);
                        }
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        mPtrView.onRefreshComplete();
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        mPtrView.onRefreshComplete();
                    }
                });

        attachDestroyFutures(future);
    }


    /**
     *
     * @author Zhenshui.Xia
     *
     */
    private class BannerPageChangeListener implements BaseViewPager.OnPageChangeListener {
        //
        public static final int SCROLL_STATE_IDLE = 0;
        //
        public static final int SCROLL_STATE_DRAGGING = 1;
        //
        public static final int SCROLL_STATE_SETTLING = 2;

        private int mSize;

        public BannerPageChangeListener(int size) {
            this.mSize = size;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case SCROLL_STATE_IDLE:
                    break;

                case SCROLL_STATE_DRAGGING:
                    break;

                case SCROLL_STATE_SETTLING:
                    break;
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            int index = position % mSize;
            mLvpIndicator.refreshIndicator(index);

        }

    }

    /**
     * 发送评论
     */
    @OnClick(R.id.comment_id_send)
    public void send() {
        String content = mEtContent.getText().toString().trim();
        if(UtilString.isBlank(content)) {
            UtilOuer.toast(this, R.string.share_comment_content_empty);
            return;
        }

        UtilOuer.hideInputManager(this);
        String commentId = mReplyComment == null? null : mReplyComment.getId();
        AgnettyFuture future = OuerApplication.mOuerFuture.shareOrderAddComment(mFollow.getId(), content, commentId,
                new OuerFutureListener(this){
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        mEtContent.setText("");
                        if(mReplyComment == null) {
                            UtilOuer.toast(ShareOrderDetailActivity.this, R.string.share_comment_success);
                        } else {
                            UtilOuer.toast(ShareOrderDetailActivity.this, R.string.share_comment_reply_success);
                        }

                        mEtContent.setHint(R.string.share_comment_content_empty);
                        mReplyComment = null;
                        getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);
                        OuerDispatcher.sendShareOrderCommentBroadcast(ShareOrderDetailActivity.this, mFollow.getId());
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        if(mReplyComment == null) {
                            UtilOuer.showExceptionToast(ShareOrderDetailActivity.this,
                                    result, R.string.share_comment_failure);
                        } else {
                            UtilOuer.showExceptionToast(ShareOrderDetailActivity.this,
                                    result, R.string.share_comment_reply_failure);
                        }
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }
                });

        attachDestroyFutures(future);
    }


    public void setReply(Comment comment) {
        mEtContent.setText("");
        mReplyComment = comment;
        mEtContent.setHint(getString(R.string.share_comment_content_reply, comment.getUser().getNick()));
    }


    private void postRead(String id, int type) {
        AgnettyFuture future = OuerApplication.mOuerFuture.postRead(id, type, null);
        attachDestroyFutures(future);
    }

}
